package com.kbt.activity;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.listener.ClickActStartActForResultListener;
import com.kbt.model.data.JiFenBean;
import com.kbt.model.data.JiFenBeans;
import com.kbt.model.data.UserJFInfo;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: KBT_ANDRIOD
 * @Copyright: KJ Co., Ltd. Copyright 2015-2020,  All rights reserved
 * @Description: ${todo}我的积分
 * @author: zhgk
 * @data: 2015/8/17 15:45
 * @version: V1.0
 */
public class MyInfoJFActivity extends Activity implements RequestManager.ResponseInterface{

    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private JiFenBeans jiFenBeans;
    private JiFenBean jiFenBean;
    private SharePreferenceUtils share;
    private String phone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.info_jf);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);
        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //不创建地址
                setResult(RESULT_CANCELED, null);
                BaseApplication.getInstance().removeActivity(MyInfoJFActivity.this);
                finish();
            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("我的积分");
    }


    @Override
    protected void onStart() {
        super.onStart();
        BaseApplication.getInstance().addActivity(MyInfoJFActivity.this);
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        share = new SharePreferenceUtils(MyInfoJFActivity.this);
        phone = share.getString("user_mobile","");
        TextView jfDesc = (TextView) findViewById(R.id.jf_desc);
        jfDesc.setText(String.valueOf("1.积分权益：在购物时每100积分抵扣现金1元\n2.积分来源：每消费1元获赠1积分\n3.敬请期待更多积分来源"));
        //获取数据
        httpData();

    }

    /**
     * 初始化视图
     */
    private void initView() {

        TextView jfNowView = (TextView) findViewById(R.id.jf_now_View);
        if(Integer.parseInt(jiFenBean.getAvailable_point())<0){
            jfNowView.setText("0");
        }else {
            jfNowView.setText(String.valueOf(jiFenBean.getAvailable_point()));
        }

        TextView jfTotalView = (TextView) findViewById(R.id.jf_total_View);
        jfTotalView.setText(String.valueOf(jiFenBean.getTotal_point()));

        TextView jfUsedView = (TextView) findViewById(R.id.jf_used_View);
        jfUsedView.setText(String.valueOf(jiFenBean.getHas_exchanged_point()));
        //超链接
//        jfUsedView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//
//        jfUsedView.setOnClickListener(new ClickActStartActForResultListener(
//                this, GoodListForJfActivity.class, StaticData.MY_FOR_JF, null));


        TextView jfLoginView = (TextView) findViewById(R.id.jf_login_getView);
        jfLoginView.setText(String.valueOf(jiFenBean.getLogin_point()));


        TextView jfCostView = (TextView) findViewById(R.id.jf_cost_getView);
        jfCostView.setText(String.valueOf(jiFenBean.getCost_point()));


        TextView jfShareView = (TextView) findViewById(R.id.jf_share_getView);
        jfShareView.setText(String.valueOf(jiFenBean.getFriend_point()));

        TextView otherView = (TextView) findViewById(R.id.jf_otherGetView);
        otherView.setText(String.valueOf(jiFenBean.getQd_point()));


    }

    /***
     * 获取数据
     */
    private void httpData() {
        if(Utils.isNetworkAvailable(MyInfoJFActivity.this)){
           mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectJiFenURL+phone, Constants.RequestTag.selectJiFenTag, map, JiFenBeans.class);
        }else{
            Toast.makeText(MyInfoJFActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.selectJiFenTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                jiFenBeans = (JiFenBeans) parsedGSON;
                if(jiFenBeans.getData()!=null){
                    jiFenBean =jiFenBeans.getData();
                    initView();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(MyInfoJFActivity.this,R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
}
